import BaseController from "../../cfw/mvc/BaseController";
import ModuleManager, { ModuleID } from "../../cfw/module/ModuleManager";
import { UIIndex, ItemState } from "../../cfw/tools/Define";
import DecorationControllerInterface from "../../public/interface/DecorationControllerInterface";
import GlobalEvent from "../../cfw/event/GlobalEvent";
import { EventName, GameText } from "../../public/Config";
import CookeryManager from "./CookeryManager";
import LobbyController from "../lobby/LobbyController";
import TextureInfo from "../../engine/comps/TextureInfo";
import CookieToyModel from "./model/CookieToyModel";
import SDKManager from "../../sdk/SDKManager";
import { ResultState } from "../../sdk/SDKConfig";
import TipController from "../tip/TipController";
import BottleItemModel from "./model/BottleItemModel";


export default class CookeryController extends BaseController implements DecorationControllerInterface {


    private static ins: CookeryController;

    private dataUrl: string;

    protected textureInfo: TextureInfo;
    static instance() {
        if (!this.ins) {
            this.ins = new CookeryController()
        }
        return this.ins;
    }

    setTextureInfo(info: TextureInfo) {
        this.textureInfo = info;
    }

    getTextureInfo() {
        return this.textureInfo;
    }


    setDataUrl(url: string) {
        this.dataUrl = url;
    }

    getDataUrl() {
        return this.dataUrl;
    }

    intoLayer() {
        ModuleManager.setModuleID(ModuleID.COOKIE)
        this.pushView('prefabs/CookeryRoomView', 'CookeryRoomView', CookeryManager.instance(), ModuleManager.getLoader(), UIIndex.ROOT)

    }

    selectColumn(model: any) {

    }

    onButtonIconClick(model: CookieToyModel) {
        if (!model.isOpen()) {
            TipController.instance().showToast(GameText.ITEM_TOAST, { num: model.getOpenLevel() })
            return;
        }
        if (model.needBuy()) {
            SDKManager.getChannel().showRewardAd(0, (s: ResultState) => {
                if (s == ResultState.YES) {
                    model.setState(ItemState.GOT)
                }
            })
        } else {
            GlobalEvent.instance().emit(EventName.SELECT_COLUMN, model)
        }

    }

    back() {
        LobbyController.instance().intoLayer()
    }

    openItem(model: BottleItemModel) {
        SDKManager.getChannel().showRewardAd(0, (state: ResultState) => {
            cc.log('CookeryController showRewardAd  state == ', state)
            if (state == ResultState.YES) {
                model.setState(ItemState.GOT)
            }
        })
    }
}
